// pages/orders/orders.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    viewLeft:0,
    viewHeight:0,
    storeName:'', // 店铺名称
    orderList:[
      {dType:'商家配送',// 配送类型
      claimNo:1996,// 取货码
      cStatus:1, // 配送状态 1=>备货状态 2=>配送中 3=>订单已完成
      commLists:[{cName:'广州大锅巴1',payNumber:5,price:1.95,oPrice:2.5,dt:7.8,imageUrl:'../../images/1.jpg',payMoney:9.75},
      {cName:'广州大锅巴2',payNumber:2,price:3,oPrice:3,dt:0,imageUrl:'../../images/1.jpg',payMoney:6},
      {cName:'广州大锅巴3',payNumber:5,price:4.25,oPrice:5,dt:8.5,imageUrl:'../../images/1.jpg',payMoney:21.25},
      {cName:'广州大锅巴4',payNumber:3,price:1.30,oPrice:2,dt:6.5,imageUrl:'../../images/1.jpg',payMoney:3.9},
      {cName:'广州大锅巴5',payNumber:7,price:5.7,oPrice:6,dt:9.5,imageUrl:'../../images/1.jpg',payMoney:39.9},
      {cName:'广州大锅巴6',payNumber:1,price:6,oPrice:8,dt:7.5,imageUrl:'../../images/1.jpg',payMoney:6},
      ],
      orderId:'202207201036528813',
      payTime:'2022-07-19 21:23:51',
      sumPayMoney:83.78,// 付款总额
      },
      {dType:'商家配送',// 配送类型
      claimNo:1997,// 取货码
      cStatus:2, // 配送状态 1=>备货状态 2=>配送中 3=>订单已完成
      commLists:[{cName:'广州大锅巴1',payNumber:5,price:1.18,oPrice:2.5,dt:7.8,imageUrl:'../../images/2.jpg',payMoney:5.5},
        {cName:'广州大锅巴2',payNumber:2,price:1.11,oPrice:3,dt:5.5,imageUrl:'../../images/2.jpg',payMoney:6},
        {cName:'广州大锅巴3',payNumber:5,price:2.31,oPrice:5,dt:8.5,imageUrl:'../../images/2.jpg',payMoney:7},
        {cName:'广州大锅巴4',payNumber:3,price:1.16,oPrice:2,dt:6.5,imageUrl:'../../images/2.jpg',payMoney:8},
        {cName:'广州大锅巴5',payNumber:7,price:3.12,oPrice:6,dt:9.5,imageUrl:'../../images/2.jpg',payMoney:7},
        {cName:'广州大锅巴6',payNumber:1,price:4.11,oPrice:8,dt:7.5,imageUrl:'../../images/2.jpg',payMoney:10},
      ],
      orderId:'202207201036528814',
      payTime:'2022-07-20 18:25:31',
      sumPayMoney:62.88,// 付款总额
      },

      {dType:'商家配送',// 配送类型
      claimNo:1997,// 取货码
      cStatus:2, // 配送状态 1=>备货状态 2=>配送中 3=>订单已完成
      commLists:[{cName:'广州大锅巴1',payNumber:5,price:2.5,oPrice:2.5,dt: 0,imageUrl:'../../images/2.jpg',payMoney:12.5},
        {cName:'广州大锅巴2',payNumber:2,price:3,oPrice:3,dt:0,imageUrl:'../../images/2.jpg',payMoney:6},
      ],
      orderId:'202207201036528815',
      payTime:'2022-07-20 18:25:31',
      sumPayMoney:18.50,// 付款总额
      },

      {dType:'商家配送',// 配送类型
      claimNo:1998,// 取货码
      cStatus:3, // 配送状态 1=>备货状态 2=>配送中 3=>订单已完成
      commLists:[{cName:'广州大锅巴1',payNumber:5,price:1.18,oPrice:2.5,dt:7.8,imageUrl:'../../images/2.jpg',payMoney:5.5},
        {cName:'广州大锅巴2',payNumber:2,price:1.11,oPrice:3,dt:5.5,imageUrl:'../../images/1.jpg',payMoney:6},
        {cName:'广州大锅巴3',payNumber:5,price:2.31,oPrice:5,dt:8.5,imageUrl:'../../images/2.jpg',payMoney:7},
        {cName:'广州大锅巴4',payNumber:3,price:1.16,oPrice:2,dt:6.5,imageUrl:'../../images/1.jpg',payMoney:8},
        {cName:'广州大锅巴5',payNumber:7,price:3.12,oPrice:6,dt:9.5,imageUrl:'../../images/2.jpg',payMoney:7},
        {cName:'广州大锅巴6',payNumber:1,price:4.11,oPrice:8,dt:7.5,imageUrl:'../../images/1.jpg',payMoney:10},
      ],
      orderId:'202207201036528814',
      payTime:'2022-07-19 18:15:25',
      sumPayMoney:52.88,// 付款总额
      },
    ],
    // -------订单详情页---------
    sumPayMoney: 0, // 合计
    dtSumMoney:0, // 总优惠
    storeInfo:{}, // 店铺信息 
    orderOnlyInfo:{}, // 单个订单的所有信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const systemInfo = wx.getSystemInfoSync(),
    height = systemInfo.windowHeight; // 获取系统信息
    this.setData({viewHeight:height});
    if(options && 'storeInfo' in options){
      const storeInfo = JSON.parse(options.storeInfo);
      this.setData({storeInfo:storeInfo}); 
    }
  },
  // 订单被点击
  cardContentTap(e){
    const orderId = e.currentTarget.dataset.orderid;
    const orderInfo = this.getOnlyOrderInfo(orderId); // 获取订单信息
    this.setData({orderOnlyInfo:orderInfo});

    this.setViewLeft(100);// 显示单个订单的详细信息
    console.log('我被点击了','订单信息:',orderInfo);
    this.calcAllCommodityAllPayMoney(orderInfo); // 计算所有商品总价格
  },
  // 0=> 全部订单 1=> 单个订单的详细信息
  setViewLeft(n){
    this.setData({viewLeft:n});
  },
  // 单个订单详情页点击返回
  return_orders(){
    this.setViewLeft(0);
  },
  // 获得某个订单下的所有已购商品信息
  getOnlyOrderInfo(orderId){
    const orderList = this.data.orderList;
    let order = {};
    for(let i in orderList){
      if(orderList[i].orderId == orderId){
        order = orderList[i];
        break;
      }
    }
    return order;
  },
  // 计算订单列表 所有商品的合计价格
  calcAllCommodityAllPayMoney(oList){
    const orderOnlyList = oList.commLists;
    let sumMoney=0, // 合计
      dtSumMoney=0; // 总优惠价格
    for(let i in orderOnlyList){
      const orderItem = orderOnlyList[i];
      sumMoney += orderItem.payMoney;
      const oPriceSum = orderItem.oPrice * orderItem.payNumber; // 原价
      dtSumMoney += (oPriceSum > orderItem.payMoney?oPriceSum - orderItem.payMoney:0); // 得到优惠
    }
    this.setData({sumPayMoney:sumMoney,dtSumMoney:dtSumMoney});
  },

  onReady() {
    
  },
  onShow() {

  },

  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.log('下拉了');
    setTimeout(()=>{
      wx.stopPullDownRefresh()
    },500)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})